package com.kai.rpc.provider.dispatch;

import com.kai.dto.RPCCommonReqDTO;

import javax.jws.Oneway;
import java.lang.reflect.Method;

/**
 * @author kai
 * @date 2021/3/20 8:56
 * @description:
 */
public class Dispatch {
    public static Object dispatch(Object object) {

        RPCCommonReqDTO reqDTO = (RPCCommonReqDTO) object;
        String methodName = reqDTO.getMethodName();
        Object[] args = reqDTO.getArga();
        String classPath = reqDTO.getClassPath();


        Class[] types = null;
        if (args != null) {
            types = new Class[args.length];
            for (int i = 0; i < args.length; i++) {
                types[i] = args[i].getClass();
            }
        }


        Object respObject = null;
        try {
            Class<?> classzz = Class.forName(classPath);
            Method declaredMethod = classzz.getDeclaredMethod(methodName, types);
            respObject = declaredMethod.invoke(classzz.newInstance(), args);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return respObject;
    }
}
